package com.cyy.dyeing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyy.dyeing.common.CommonUtils;
import com.cyy.dyeing.common.DyeingConstants;
import com.cyy.dyeing.common.Result;
import com.cyy.dyeing.common.TokenUtils;
import com.cyy.dyeing.model.Plan;
import com.cyy.dyeing.dto.PlanDto;
import com.cyy.dyeing.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/7/26
 */
@RequestMapping("/plan")
@RestController
public class PlanController {

    @Autowired
    private BaseApi baseApi;

    @Autowired
    private PlanService planService;

    @GetMapping("/year-month-list")
    public Result getYearMonthList(){
        return Result.ok(CommonUtils.getYearMonthList());
    }

    @GetMapping("/info")
    public Result getInfos(){
        return baseApi.infos();
    }

    /**
     * 根据条件查询计划列表
     * @param planDto
     * @return
     */
    @GetMapping("/list")
    public Result planList(PlanDto planDto){
        Page<Plan> planPage = new Page<>(planDto.getPageNum(), planDto.getPageSize());
        List<Plan> pl =  planService.planList(planDto);
        planPage.setRecords(pl);
        planService.page(planPage);
        return Result.ok(planPage);
    }

    /**
     * 获取序号
     * @param planDto
     * @return
     */
    @GetMapping("/xh")
    public Result getXh(PlanDto planDto){
        Integer xh = planService.getXh(planDto);
        return Result.ok(xh);
    }

    /**
     * 添加计划
     * @param plan
     * @return
     */
    @PostMapping("/add")
    public Result addPlan(@RequestBody Plan plan){
        plan.setPlan_status(false);
        planService.save(plan);
        return Result.ok();
    }

    /**
     * 更改计划
     * @param plan
     * @return
     */
    @PutMapping("/update")
    public Result updatePlan(@RequestBody Plan plan){
        planService.updateById(plan);
        return Result.ok();
    }

    /**
     * 单个删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deletePlan(@PathVariable("id") String id){
        LambdaQueryWrapper<Plan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Plan::getPlan_id, id);
        queryWrapper.eq(Plan::getPlan_status, 0);
        planService.remove(queryWrapper);
        return Result.ok();
    }

    /**
     * 批量删除
     * @return
     */
    @DeleteMapping("/delete")
    public Result deletePlans(@RequestBody String[] ids){
        for (int i = 0; i < ids.length; i++) {
            deletePlan(ids[i]);
        }
        return Result.ok();
    }

    /**
     * 计划下达
     * @param id
     * @param clientToken
     * @return
     */
    @PutMapping("/confirm/{id}")
    public Result planConfirm(@PathVariable("id") Long id, @RequestHeader(DyeingConstants.HEADER_USER_TOKEN)String clientToken){
        PlanDto planDto = new PlanDto();
        planDto.setPlan_id(id);
        planDto.setPlan_person(TokenUtils.getCurrentUser(clientToken).getUsername());
        planDto.setPlan_date(new Date());
        planDto.setPlan_status(true);
        LambdaUpdateWrapper<Plan> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Plan::getPlan_id, id);
        planService.update(planDto, updateWrapper);
        return Result.ok();
    }

}
